package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.dto.UserDto;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.UserService;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper userMapper;
    @Override
    public PageInfo<UserDto> queryUsers(SysUser user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserDto> userDtos = userMapper.selectUsers(user);
        return new PageInfo<>(userDtos);
    }

    @Override
    public SysUser queryUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean saveOrUpdate(SysUser user) {
        if(user.getUserId()!=null){
            user.setUpdateDate(DateUtil.formatAsDatetime(new Date()));
            return userMapper.updateByPrimaryKeySelective(user)==1?true:false;
        }
        user.setCreateDate(DateUtil.formatAsDatetime(new Date()));
        return userMapper.insertSelective(user)==1?true:false;
    }

    @Override
    public boolean removeById(Integer id) {
        return userMapper.deleteByPrimaryKey(id)==1?true:false;
    }

    @Override
    public boolean removeByIds(Integer[] ids) {
        int num = 0;
        for (Integer id:ids
             ) {
            if(removeById(id)){
                num++;
            }
        }
        return num==ids.length?true:false;
    }
}
